﻿using System;
using System.Linq;
using System.Windows.Forms;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.CanBoQuanLyForm;
using ComputerVision.CapQuanLyHanhChinhForm;
using ComputerVision.CoQuanQuanLyPCCCForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.DoiForm;
using ComputerVision.HinhThucDauTuForm;
using ComputerVision.LoaiCoSoForm;
using ComputerVision.LoaiHinhHoatDongForm;
using ComputerVision.NganhForm;
using ComputerVision.PhongForm;
using ComputerVision.Services;
using ComputerVision.ThanhPhanKinhTeForm;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmPhieuDieuTraCoSoDetail : frmObject
    {
        private readonly Guid _idPhieu;
        private readonly IService<Phong> _phong;
        private readonly IService<Doi> _doi;
        private readonly IService<CapQuanLyHanhChinh> _capQuanLyHanhChinh;
        private readonly IService<CanBo> _canBo;
        private IService<LoaiCoSo> _loaiCoSo;
        private readonly IService<CoQuanQuanLyPCCC> _coQuanQuanLyPccc;
        private readonly IService<HinhThucDauTu> _hinhThucDauTu;
        private readonly IService<LoaiHinhHoatDong> _loaiHinhHoatDong;
        private readonly IService<ThanhPhanKinhTe> _thanhPhanKinhTe;
        private readonly IService<Nganh> _nganh;
        private IService<ThamDuyetPCCC> _thamDuyet;
        private IService<HangMuc> _hangMuc;
        private IService<PhuongTienCoSo> _phuongTienCoSo;
        private IService<NguonNuoc> _nguonNuoc;
        private IService<TuyenTruyenHuanLuyen> _tuyenTruyenHuanLuyen;
        private IService<KiemTraAnToan> _kiemTraAnToan;
        private IService<PhuongAnChuaChay> _phuongAnChuaChay;
        private IService<XuLyViPham> _xuLyViPham;
        private readonly IService<PhieuDieuTraCoSo> _phieuDieuTraCoSo;

        public frmPhieuDieuTraCoSoDetail()
        {
            InitializeComponent();
            _idPhieu = Guid.NewGuid();
            _phong = Program.Container.Resolve<IService<Phong>>();
            _doi = Program.Container.Resolve<IService<Doi>>();
            _capQuanLyHanhChinh = Program.Container.Resolve<IService<CapQuanLyHanhChinh>>();
            _coQuanQuanLyPccc = Program.Container.Resolve<IService<CoQuanQuanLyPCCC>>();
            _canBo = Program.Container.Resolve<IService<CanBo>>();
            _loaiCoSo = Program.Container.Resolve<IService<LoaiCoSo>>();
            _hinhThucDauTu = Program.Container.Resolve<IService<HinhThucDauTu>>();
            _loaiHinhHoatDong = Program.Container.Resolve<IService<LoaiHinhHoatDong>>();
            _thanhPhanKinhTe = Program.Container.Resolve<IService<ThanhPhanKinhTe>>();
            _nganh = Program.Container.Resolve<IService<Nganh>>();
            _phuongTienCoSo = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            _nguonNuoc = Program.Container.Resolve<IService<NguonNuoc>>();
            _tuyenTruyenHuanLuyen = Program.Container.Resolve<IService<TuyenTruyenHuanLuyen>>();
            _kiemTraAnToan = Program.Container.Resolve<IService<KiemTraAnToan>>();
            _phuongAnChuaChay = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            _xuLyViPham = Program.Container.Resolve<IService<XuLyViPham>>();
            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();

            txtSo.Text = Constants.SoDetail.TenSo;
            LoadPhong();
            LoadDoi();
            LoadCoQuanQl();
            LoadCanBo();
            LoadLoaiCoSo();
            LoadHinhThucDauTu();
            LoadLoaiHinhHoatDong();
            LoadThanhPhanKinhTe();
            LoadNganh();
            LoadCapQuanLyHanhChinh();

            //Insert temp record
            InsertTempRecord();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            //this.Close();
            var frm = new frmPhieuDieuTraCoSoList();
            frm.MdiParent = MdiParent;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void btnPhong_Click(object sender, EventArgs e)
        {
            var frm = new frmPhongDetail(this);
            frm.ShowDialog();
        }

        private void btnAddDoi_Click(object sender, EventArgs e)
        {
            var frm = new frmDoiDetail(this);
            frm.ShowDialog();
        }

        private void btnAddCoQuanQL_Click(object sender, EventArgs e)
        {
            var frm = new frmCoQuanQuanLyPCCCDetail(this);
            frm.ShowDialog();
        }

        private void btnAddCanBoQuanLy_Click(object sender, EventArgs e)
        {
            var frm = new frmCanBoQuanLyDetail(this);
            frm.ShowDialog();
        }

        private void btnAddLoaiCoSo_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiCoSoDetail(this);
            frm.ShowDialog();
        }

        private void btnAddCapQuanLyHanhChinh_Click(object sender, EventArgs e)
        {
            var frm = new frmCapQuanLyHanhChinhDetail(this);
            frm.ShowDialog();
        }

        private void btnAddHinhThucDauTu_Click(object sender, EventArgs e)
        {
            var frm = new frmHinhThucDauTuDetail(this);
            frm.ShowDialog();
        }

        private void btnAddLoaiHinhHoatDong_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiHinhHoatDongDetail(this);
            frm.ShowDialog();
        }

        private void btnAddThanhPhanKinhTe_Click(object sender, EventArgs e)
        {
            var frm = new frmThanhPhanKinhTeDetail(this);
            frm.ShowDialog();
        }

        private void btnAddNganh_Click(object sender, EventArgs e)
        {
            var frm = new frmNganhDetail(this);
            frm.ShowDialog();
        }

        #region Insert temp record
        private void InsertTempRecord()
        {
            try
            {
                var model = new PhieuDieuTraCoSo
                {
                    Id = _idPhieu,
                    Status = false
                };
                _phieuDieuTraCoSo.Create(model);
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Load data to combobox

        public void LoadPhong()
        {
            drlPhong.DataSource = _phong.ToList().ToList().Select(p => new { p.Id, p.TenPhong }).ToList();
            drlPhong.DisplayMember = "TenPhong";
            drlPhong.ValueMember = "Id";
        }

        public void LoadDoi()
        {
            drlDoi.DataSource = _doi.ToList().ToList().Select(p => new { p.Id, p.TenDoi }).ToList();
            drlDoi.DisplayMember = "TenDoi";
            drlDoi.ValueMember = "Id";
        }

        public void LoadCoQuanQl()
        {
            var source = _coQuanQuanLyPccc.ToList().ToList().Select(p => new { p.Id, p.Ten }).ToList();

            drlCoQuanQL.DataSource = source;
            drlCoQuanQL.DisplayMember = "Ten";
            drlCoQuanQL.ValueMember = "Id";
        }

        public void LoadCanBo()
        {
            drlCanBoQuanLy.DataSource = _canBo.ToList().ToList().Select(p => new { p.Id, p.HoTen }).ToList();
            drlCanBoQuanLy.DisplayMember = "HoTen";
            drlCanBoQuanLy.ValueMember = "Id";
        }

        public void LoadLoaiCoSo()
        {
            drlLoaiCoSo.DataSource = _loaiCoSo.ToList().ToList().Select(p => new { p.Id, p.Ten }).ToList();
            drlLoaiCoSo.DisplayMember = "Ten";
            drlLoaiCoSo.ValueMember = "Id";
        }

        public void LoadCapQuanLyHanhChinh()
        {
            var source = _capQuanLyHanhChinh.ToList().ToList().Select(p => new { p.Id, p.Ten }).ToList();

            drlCapQuanLyHanhChinh.DataSource = source;
            drlCapQuanLyHanhChinh.DisplayMember = "Ten";
            drlCapQuanLyHanhChinh.ValueMember = "Id";
        }

        public void LoadHinhThucDauTu()
        {
            drlHinhThucDauTu.DataSource = _hinhThucDauTu.ToList().ToList().Select(p => new { p.Id, TenHinhThuc = p.Ten }).ToList();
            drlHinhThucDauTu.DisplayMember = "TenHinhThuc";
            drlHinhThucDauTu.ValueMember = "Id";
        }

        public void LoadLoaiHinhHoatDong()
        {
            drlLoaiHinhHoatDong.DataSource = _loaiHinhHoatDong.ToList().ToList().Select(p => new { p.Id, TenLoaiHinh = p.Ten }).ToList();
            drlLoaiHinhHoatDong.DisplayMember = "TenLoaiHinh";
            drlLoaiHinhHoatDong.ValueMember = "Id";
        }

        public void LoadThanhPhanKinhTe()
        {
            drlThanhPhanKinhTe.DataSource = _thanhPhanKinhTe.ToList().ToList().Select(p => new { p.Id, TenTP = p.Ten }).ToList();
            drlThanhPhanKinhTe.DisplayMember = "TenTP";
            drlThanhPhanKinhTe.ValueMember = "Id";
        }

        public void LoadNganh()
        {
            drlNganh.DataSource = _nganh.ToList().ToList().Select(p => new { p.Id, TenNganh = p.Ten }).ToList();
            drlNganh.DisplayMember = "TenNganh";
            drlNganh.ValueMember = "Id";
        }


        #endregion

        #region ThamDuyetPCCC

        protected void LoadDataThamDuyet()
        {
            _thamDuyet = Program.Container.Resolve<IService<ThamDuyetPCCC>>();
            thamDuyetPCCCBindingSource.DataSource = _thamDuyet.ToList().ToList().Select(p => new { p.Id, p.SoVanBan, VanBanThamDuyetPCCC = p.VanBanThamDuyetPCCC.Ten, p.NgayCap, CoQuanCapVanBanThamDuyet = p.CoQuanCapVanBanThamDuyet.Ten, p.GhiChu, p.PhieuDieuTraCoSoId }).Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu)).ToList();
        }

        private void btnAddThamDuyet_Click(object sender, EventArgs e)
        {
            using (var frm = new frmThamDuyetDetail())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataThamDuyet;
                frm.ShowDialog();
            }
        }

        private void btnEditThamDuyet_Click(object sender, EventArgs e)
        {
            if (grvThamDuyet.SelectedRows.Count > 0)
            {
                var selectedRow = grvThamDuyet.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmThamDuyetDetail(id))
                {
                    fEdit.OnLoadData += LoadDataThamDuyet;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelThamDuyet_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvThamDuyet.SelectedRows.Count != 0)
                {
                    var id = Guid.Parse(grvThamDuyet.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                    {
                        var item = _thamDuyet.Detail(id);
                        _thamDuyet.Delete(item);
                        LoadDataThamDuyet();
                    }
                }
                else
                {
                    AlertList.Error(Constants.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btnRefreshThamDuyet_Click(object sender, EventArgs e)
        {
            LoadDataThamDuyet();
        }

        #endregion

        #region HangMuc

        protected void LoadDataHangMuc()
        {
            _hangMuc = Program.Container.Resolve<IService<HangMuc>>();
            hangMucBindingSource.DataSource = _hangMuc.ToList().ToList().Select(p => new { p.Id, p.TenCongTrinh, TenHangSanXuat = p.HangSanXuat.Ten, TenBacChiuLua = p.BacChiuLua.Ten, p.DienTichM2, p.SoTang, p.DienTichM3, p.SoPhong, p.TenChatChay, p.DienTichChatChayM2, p.PhieuDieuTraCoSo_Id }).ToList().Where(a => a.PhieuDieuTraCoSo_Id.Equals(_idPhieu)).ToList();
        }

        private void btnAddHangMuc_Click(object sender, EventArgs e)
        {
            using (var frm = new frmHangMuc())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataHangMuc;
                frm.ShowDialog();
            }
        }

        private void btnEditHangMuc_Click(object sender, EventArgs e)
        {
            if (grvHangMuc.SelectedRows.Count > 0)
            {
                var selectedRow = grvHangMuc.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmHangMuc(id))
                {
                    fEdit.OnLoadData += LoadDataHangMuc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnXoaHangMuc_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvHangMuc.SelectedRows.Count != 0)
                {
                    var id = Guid.Parse(grvHangMuc.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                    {
                        var item = _hangMuc.Detail(id);
                        _hangMuc.Delete(item);
                        LoadDataHangMuc();
                    }
                }
                else
                {
                    AlertList.Error(Constants.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btnRefreshHangMuc_Click(object sender, EventArgs e)
        {
            LoadDataHangMuc();
        }


        #endregion

        #region PhuongTienPCCC

        protected void LoadDataPhuongTienPcCc()
        {
            _phuongTienCoSo = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            phuongTienCoSoBindingSource.DataSource = _phuongTienCoSo.ToList().ToList().Select(p => new { p.Id, p.Ten, p.SoLuong, p.TinhTrangHoatDong_Tot, p.TinhTrangHoatDong_TrungBinh, p.TinhTrangHoatDong_Kem, p.TinhTrangHoatDong_HuHong, p.Vitri, p.PhieuDieuTraCoSoId }).ToList().Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu)).ToList();
        }

        private void btnAddPhuongTienPCCC_Click(object sender, EventArgs e)
        {
            using (var frm = new frmPhuongTienPCCC())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataPhuongTienPcCc;
                frm.ShowDialog();
            }
        }

        private void btnEditPhuongTien_Click(object sender, EventArgs e)
        {
            if (grvPhuongTien.SelectedRows.Count > 0)
            {
                var selectedRow = grvPhuongTien.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmPhuongTienPCCC(id))
                {
                    fEdit.OnLoadData += LoadDataPhuongTienPcCc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDeletePhuongTien_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvPhuongTien.SelectedRows.Count != 0)
                {
                    var id = Guid.Parse(grvPhuongTien.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                    {
                        var item = _phuongTienCoSo.Detail(id);
                        _phuongTienCoSo.Delete(item);
                        LoadDataPhuongTienPcCc();
                    }
                }
                else
                {
                    AlertList.Error(Constants.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btnRefreshPhuongTien_Click(object sender, EventArgs e)
        {
            LoadDataPhuongTienPcCc();
        }

        #endregion

        #region NguonNuoc

        protected void LoadDataNguonNuoc()
        {
            _nguonNuoc = Program.Container.Resolve<IService<NguonNuoc>>();
            nguonNuocBindingSource.DataSource = _nguonNuoc.ToList().ToList().Select(p => new { p.Id, p.Ten, p.KhoiTich, p.MBCC, p.NgoaiCoSo, PhanLoaiChiTietNguonNuoc = p.PhanLoaiChiTietNguonNuoc.Ten, PhanLoaiChiTietNguonNuocId = p.PhanLoaiChiTietNguonNuoc.LoaiNguonNuoc.Ten, p.PhieuDieuTraCoSoesId, p.XeCC, p.TrongCoSo }).ToList().Where(a => a.PhieuDieuTraCoSoesId.Equals(_idPhieu)).ToList();
        }

        private void btnAddNguonNuoc_Click(object sender, EventArgs e)
        {
            using (var frm = new frmNguonNuoc())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataNguonNuoc;
                frm.ShowDialog();
            }
        }

        private void btnEditNguonNuoc_Click(object sender, EventArgs e)
        {
            if (dgvNguonNuoc.SelectedRows.Count > 0)
            {
                var selectedRow = dgvNguonNuoc.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmNguonNuoc(id))
                {
                    fEdit.OnLoadData += LoadDataNguonNuoc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelNguonNuoc_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvNguonNuoc.SelectedRows.Count != 0)
                {
                    var id = Guid.Parse(dgvNguonNuoc.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                    {
                        var item = _nguonNuoc.Detail(id);
                        _nguonNuoc.Delete(item);
                        LoadDataNguonNuoc();
                    }
                }
                else
                {
                    AlertList.Error(Constants.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btnRefreshNguonNuoc_Click(object sender, EventArgs e)
        {
            LoadDataNguonNuoc();
        }

        #endregion

        #region TuyenTruyenHuanLuyen

        protected void LoadDataTuyenTruyen()
        {
            _tuyenTruyenHuanLuyen = Program.Container.Resolve<IService<TuyenTruyenHuanLuyen>>();
            tuyenTruyenHuanLuyenBindingSource.DataSource = _tuyenTruyenHuanLuyen.ToList().ToList().Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu)).ToList();
        }

        private void btAddTuyenTruyen_Click(object sender, EventArgs e)
        {
            using (var frm = new frmTuyenTruyenHuanLuyen())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataTuyenTruyen;
                frm.ShowDialog();
            }
        }

        private void btEditTuyenTruyen_Click(object sender, EventArgs e)
        {
            if (grvTuyenTruyenHuanLuyen.SelectedRows.Count > 0)
            {
                var selectedRow = grvTuyenTruyenHuanLuyen.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmTuyenTruyenHuanLuyen(id))
                {
                    fEdit.OnLoadData += LoadDataTuyenTruyen;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btDelTuyenTruyen_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvTuyenTruyenHuanLuyen.SelectedRows.Count != 0)
                {
                    var id = Guid.Parse(grvTuyenTruyenHuanLuyen.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                    {
                        var item = _tuyenTruyenHuanLuyen.Detail(id);
                        _tuyenTruyenHuanLuyen.Delete(item);
                        LoadDataTuyenTruyen();
                    }
                }
                else
                {
                    AlertList.Error(Constants.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btRefreshTuyenTruyen_Click(object sender, EventArgs e)
        {
            LoadDataTuyenTruyen();

        }

        #endregion

        #region KiemTraAnToan

        protected void LoadDataKiemTraAnToan()
        {
            _kiemTraAnToan = Program.Container.Resolve<IService<KiemTraAnToan>>();
            kiemTraAnToanBindingSource.DataSource = _kiemTraAnToan.ToList().ToList().Where(a => a.PhieuDieuTraCoSoesId.Equals(_idPhieu)).ToList();
        }

        private void btnAddAnToan_Click(object sender, EventArgs e)
        {
            using (var frm = new frmKiemTraAnToan())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataKiemTraAnToan;
                frm.ShowDialog();
            }
        }

        private void btnEditAnToan_Click(object sender, EventArgs e)
        {
            if (grvAnToan.SelectedRows.Count > 0)
            {
                var selectedRow = grvAnToan.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmKiemTraAnToan(id))
                {
                    fEdit.OnLoadData += LoadDataKiemTraAnToan;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelAnToan_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvAnToan.SelectedRows.Count != 0)
                {
                    var id = Guid.Parse(grvAnToan.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                    {
                        var item = _kiemTraAnToan.Detail(id);
                        _kiemTraAnToan.Delete(item);
                        LoadDataKiemTraAnToan();
                    }
                }
                else
                {
                    AlertList.Error(Constants.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btnRefreshAnToan_Click(object sender, EventArgs e)
        {
            LoadDataKiemTraAnToan();

        }

        #endregion

        #region PhuongAnCC

        private void LoadDataPhuongAnCc()
        {
            _phuongAnChuaChay = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            phuongAnChuaChayBindingSource.DataSource = _phuongAnChuaChay.ToList().ToList().Select(p => new { p.Id, p.Ngay, p.TinhHuong, LucLuongThucTap = p.LucLuongThucTap.Ten, PhuongTienThucTap = p.PhuongTienThucTap.Ten, p.LucLuong_SoLuong, p.PhuongTien_SoLuong, p.KienNghi, p.KetQua, p.PhieuDieuTraCoSoId }).Where(a => a.PhieuDieuTraCoSoId.Equals(_idPhieu)).ToList();
        }

        private void btnAddPhuongAnCC_Click(object sender, EventArgs e)
        {
            using (var frm = new frmPhuongAnCC())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataPhuongAnCc;
                frm.ShowDialog();
            }
        }

        private void btnEditPhuongAnCC_Click(object sender, EventArgs e)
        {
            if (dgvPhuongAnCc.SelectedRows.Count > 0)
            {
                var selectedRow = dgvPhuongAnCc.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmPhuongAnCC(id))
                {
                    fEdit.OnLoadData += LoadDataPhuongAnCc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelPhuongAnCC_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvPhuongAnCc.SelectedRows.Count != 0)
                {
                    var id = Guid.Parse(dgvPhuongAnCc.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                    {
                        var item = _phuongAnChuaChay.Detail(id);
                        _phuongAnChuaChay.Delete(item);
                        LoadDataPhuongAnCc();
                    }
                }
                else
                {
                    AlertList.Error(Constants.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btnRefreshPhuongAnCC_Click(object sender, EventArgs e)
        {
            LoadDataPhuongAnCc();
        }

        #endregion

        #region XuLyViPham

        private void LoadDataXuLyViPham()
        {
            _xuLyViPham = Program.Container.Resolve<IService<XuLyViPham>>();
            xuLyViPhamBindingSource.DataSource = _xuLyViPham.ToList().ToList().Select(p => new { p.Id, p.Ngay, p.DoiTuongViPham, NhomHanhVi = p.NhomHanhVi.Ten, p.NoiDung, p.CanhCao, p.PhatTien, p.NgayTamDinhChi, p.NgayPhucHoi, p.DinhChi, p.BienPhapXuLyKhac, p.PhieuDieuTraCoSoesId }).Where(a => a.PhieuDieuTraCoSoesId.Equals(_idPhieu)).ToList();
        }

        private void btnAddXuLyViPham_Click(object sender, EventArgs e)
        {
            using (var frm = new frmXuLyViPham())
            {
                frm.IdPhieu = _idPhieu;
                frm.OnLoadData += LoadDataXuLyViPham;
                frm.ShowDialog();
            }
        }

        private void btnEditXuLyViPham_Click(object sender, EventArgs e)
        {
            if (grvXuLyViPham.SelectedRows.Count > 0)
            {
                var selectedRow = grvXuLyViPham.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmXuLyViPham(id))
                {
                    fEdit.OnLoadData += LoadDataXuLyViPham;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }

        private void btnDelXuLyViPham_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvXuLyViPham.SelectedRows.Count != 0)
                {
                    var id = Guid.Parse(grvXuLyViPham.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constants.Messages.DeleteConfirm))
                    {
                        var item = _xuLyViPham.Detail(id);
                        _xuLyViPham.Delete(item);
                        LoadDataXuLyViPham();
                    }
                }
                else
                {
                    AlertList.Error(Constants.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void btnRefreshXuLyViPham_Click(object sender, EventArgs e)
        {
            LoadDataXuLyViPham();
        }

        #endregion
    }
}
